package com.tj.api.rest.product;

import com.tj.api.annotations.VisitorAccessible;
import com.tj.api.utils.VersionUtils;
import com.tj.bo.common.conf.api.ApiResponse;
import com.tj.bo.common.conf.config.RedisConfigUtil;
import com.tj.bo.common.conf.util.ParamsValidateUtil;
import com.tj.bo.product.client.ProductService;
import com.tj.bo.product.client.vo.ProductGlobalSetting;
import com.tj.bo.product.client.vo.ProductListVo;
import com.tj.bo.trade.client.enums.TradeTypeEnum;
import com.tj.bo.user.client.UserProductService;
import com.tj.bo.user.client.UserWanningListService;
import com.tj.template.application.product.output.ProductListResponse;
import com.tj.template.config.vo.RequestLocalInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * User: binary (mr.vencnet@gmail.com)
 * Date: 2020-06-08
 * Time: 16:30
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private UserProductService userProductService;
    @Autowired
    private RedisConfigUtil redisDbConfigUtil;
    @Autowired
    private UserWanningListService userWanningListService;

    @VisitorAccessible
    @RequestMapping("/list")
    public ApiResponse<ProductListResponse> getProductList() {
        String userCode = RequestLocalInfo.getCurrentUser().getUserCode();
        boolean isGrey = userWanningListService.isGrey(userCode);
        return new ApiResponse<>(new ProductListResponse(userProductService.getAllActiveProductList(userCode, isGrey), null));
    }

    /**
     * 统一配置接口
     *
     * @return
     */
    @VisitorAccessible
    @RequestMapping("/global/settings")
    public ApiResponse<ProductListResponse> getProductGlobalSettings(@RequestBody ProductRequest productRequest) {

        if(VersionUtils.version_more_than_1_0_1(RequestLocalInfo.getRequestBasicInfo())){
            ParamsValidateUtil.checkParams(TradeTypeEnum.getEnumByCode(productRequest.getTradeType()) == null, "tradeType is invalid");
        }
        String userCode = RequestLocalInfo.getCurrentUser().getUserCode();
        boolean isGrey = userWanningListService.isGrey(userCode);
        ProductGlobalSetting globalSetting = productService.getGlobalSetting(isGrey);
        ProductListVo allActiveProductList = userProductService.getAllActiveProductList(userCode, isGrey);
        ProductListResponse response = new ProductListResponse(allActiveProductList, productRequest.getTradeType());
        response.setChartTimeRange(globalSetting.getChartTimeRange());
        response.setCoinType(globalSetting.getCoinType());
        response.setIsAutoOrderSwitchOpen(globalSetting.getIsAutoOrderSwitchOpen());
        response.setMoneyLimit(globalSetting.getMoneyLimit());
        response.setOrderTimeRange(globalSetting.getOrderTimeRange());
//        response.setOpenRecharge(redisDbConfigUtil.getOpenRecharge());
        response.setOpenRecharge(true);
        return new ApiResponse<>(response);
    }

}
